// 测试连接到真实的MCP服务器
const { ToolManager } = require('../dist/tools/index.js');
const path = require('path');

async function testRealMCPServer() {
    const toolManager = new ToolManager();
    
    console.log('=== 🔌 真实MCP服务器连接测试 ===\n');
    
    // MCP服务器路径
    const mcpServerPath = 'E:\\mcp_server\\mcp_server\\out';
    const serverCommand = 'node index.js';
    const serverName = 'file-system-server';
    
    console.log(`📡 准备连接到MCP服务器:`);
    console.log(`   路径: ${mcpServerPath}`);
    console.log(`   命令: ${serverCommand}`);
    console.log(`   服务器名: ${serverName}\n`);
    
    try {
        // 步骤1: 检查当前连接状态
        console.log('📋 步骤1: 检查当前连接状态');
        const listResult1 = await toolManager.executeTool('mcp-client', {
            action: 'list-servers'
        });
        console.log(listResult1);
        console.log('');
        
        // 步骤2: 连接到MCP服务器
        console.log('🔗 步骤2: 连接到MCP服务器');
        const connectResult = await toolManager.executeTool('mcp-client', {
            action: 'connect',
            serverName: serverName,
            command: serverCommand,
            cwd: mcpServerPath
        });
        console.log(connectResult);
        console.log('');
        
        // 步骤3: 再次检查连接状态
        console.log('📋 步骤3: 验证连接状态');
        const listResult2 = await toolManager.executeTool('mcp-client', {
            action: 'list-servers'
        });
        console.log(listResult2);
        console.log('');
        
        // 步骤4: 列出服务器的可用工具
        console.log('🔧 步骤4: 列出可用工具');
        const toolsResult = await toolManager.executeTool('mcp-client', {
            action: 'list-tools',
            serverName: serverName
        });
        console.log(toolsResult);
        console.log('');
        
        // 步骤5: 列出服务器资源
        console.log('📚 步骤5: 列出服务器资源');
        const resourcesResult = await toolManager.executeTool('mcp-client', {
            action: 'list-resources',
            serverName: serverName
        });
        console.log(resourcesResult);
        console.log('');
        
        // 步骤6: 测试调用一个工具（如果有的话）
        console.log('⚡ 步骤6: 测试调用远程工具');
        
        // 尝试调用可能存在的工具
        const commonTools = [
            'list-directory-tree',
            'read-lines', 
            'read-file',
            'list-files',
            'get-file-info'
        ];
        
        let toolCalled = false;
        for (const toolName of commonTools) {
            try {
                console.log(`   尝试调用工具: ${toolName}`);
                const callResult = await toolManager.executeTool('mcp-client', {
                    action: 'call-tool',
                    serverName: serverName,
                    toolName: toolName,
                    toolArgs: {
                        // 使用相对安全的参数
                        dir: '.',
                        maxDepth: 2
                    }
                });
                console.log(`   ✅ 成功调用 ${toolName}:`);
                console.log(callResult);
                toolCalled = true;
                break;
            } catch (error) {
                console.log(`   ⚠️ 工具 ${toolName} 调用失败: ${error.message}`);
            }
        }
        
        if (!toolCalled) {
            console.log('   💡 没有找到可调用的通用工具，这是正常的');
        }
        console.log('');
        
        // 步骤7: 测试断开连接
        console.log('❌ 步骤7: 断开连接');
        const disconnectResult = await toolManager.executeTool('mcp-client', {
            action: 'disconnect',
            serverName: serverName
        });
        console.log(disconnectResult);
        console.log('');
        
        // 步骤8: 最终验证
        console.log('🔍 步骤8: 最终验证连接状态');
        const finalListResult = await toolManager.executeTool('mcp-client', {
            action: 'list-servers'
        });
        console.log(finalListResult);
        
        console.log('🎉 MCP服务器连接测试完成！');
        
    } catch (error) {
        console.error('❌ 测试过程中出现错误:', error);
        
        // 尝试清理连接
        try {
            await toolManager.executeTool('mcp-client', {
                action: 'disconnect',
                serverName: serverName
            });
        } catch (cleanupError) {
            console.error('清理连接时出错:', cleanupError);
        }
    }
}

// 运行测试
testRealMCPServer().catch(console.error);